package com.jtjrenren.android.taxi.driver.manger;

import android.content.Context;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;

import com.library.utils.LogUtil;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by Administrator on 2016/8/26.
 */

public class LocationManger {
    public interface LocationListener {
        void onLocation(BDLocation bdLocation);
    }

    private LocationClient locationClient;
    private LocationListener locationListener;
    private Context context;


    public void init(Context context) {
        this.context = context;
    }

    public void start() {
        if (context == null)
            return;

        if (locationClient == null) {
            locationClient = new LocationClient(context);
            locationClient.setLocOption(getBdMapOption());
            locationClient.registerLocationListener(new myBdLocationListener());
        }
        locationClient.start();
    }

    public void stop() {
        if (locationClient != null)
            locationClient.stop();
    }

    public void request() {
        if (locationClient != null)
            locationClient.requestLocation();
    }

    private class myBdLocationListener implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation bdLocation) {
            if (bdLocation == null)
                return;

            LogUtil.log(LocationManger.class, "百度定位结果：" + bdLocation.getLatitude() + "," + bdLocation.getLongitude());

            if (locationListener != null)
                locationListener.onLocation(bdLocation);

            EventBus.getDefault().post(bdLocation);

            AppManger.getInstance().currentLocation = bdLocation;
        }
    }

    public void setLocationListener(LocationListener locationListener) {
        this.locationListener = locationListener;
    }

    private LocationClientOption getBdMapOption() {
        LocationClientOption mOption = new LocationClientOption();
        mOption.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);//可选，默认高精度，设置定位模式，高精度，低功耗，仅设备
        mOption.setCoorType("bd09ll");//可选，默认gcj02，设置返回的定位结果坐标系，如果配合百度地图使用，建议设置为bd09ll;
        mOption.setScanSpan(3000);//可选，默认0，即仅定位一次，设置发起定位请求的间隔需要大于等于1000ms才是有效的
        mOption.setIsNeedAddress(true);//可选，设置是否需要地址信息，默认不需要
        mOption.setIsNeedLocationDescribe(true);//可选，设置是否需要地址描述
        mOption.setNeedDeviceDirect(false);//可选，设置是否需要设备方向结果
        mOption.setLocationNotify(false);//可选，默认false，设置是否当gps有效时按照1S1次频率输出GPS结果
        mOption.setIgnoreKillProcess(true);//可选，默认true，定位SDK内部是一个SERVICE，并放到了独立进程，设置是否在stop的时候杀死这个进程，默认不杀死
        mOption.setIsNeedLocationDescribe(true);//可选，默认false，设置是否需要位置语义化结果，可以在BDLocation.getLocationDescribe里得到，结果类似于“在北京天安门附近”
        mOption.setIsNeedLocationPoiList(true);//可选，默认false，设置是否需要POI结果，可以在BDLocation.getPoiList里得到
        mOption.SetIgnoreCacheException(false);//可选，默认false，设置是否收集CRASH信息，默认收集

        return mOption;
    }
}
